﻿using System;
using System.Web.UI;
using Eassets.Service;
using Eassets.Layered.Domain;

namespace Eassets.UI
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                labelErrorMessage.Enabled = false;
            }
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            if (!(textBoxComCode.Text == "" && textBoxEmail.Text == "" && textBoxPassword.Text == ""))
            {
                Session["LogginCompany"] = textBoxComCode.Text.Trim();
                if (CheckValidity(textBoxEmail.Text, textBoxPassword.Text))
                {
                    Response.Redirect("MainFram.aspx");
                    labelErrorMessage.Enabled = false;
                }
                else
                {
                    labelErrorMessage.Visible = true;
                    //Response.Redirect("Default.aspx");
                }
            }
        }

        protected Boolean CheckValidity(string email, string password)
        {
            EmployeeService employeeService = new EmployeeService();
            Employee employee = new Employee();

            try
            {
                employee = employeeService.GetEmployeeLoginDetails(email, password);
            }
            catch (Exception)
            { }

            if (employee != null)
            {
                string checkPassword;
                CommonService commonService = new CommonService();
                checkPassword = commonService.EncryptData(password);

                if (checkPassword == employee.InitialPassword)
                {
                    Session["LoggedUserId"] = employee.EmployeeId;
                    return true;
                }
            }
            return false;
        }

    }
}